using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Zhao.Common.Helper;
using Zhao.IServer;
using Zhao.Models.DTO;

namespace Zhao.Arena.Controllers
{
    /// <summary>
    /// 结算记录控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class SettlementRecordController : ControllerBase
    {
        private readonly ISettlementRecordService _settlementRecordService;
        private const string VenueIdClaimType = "VenueId"; // 认证令牌中场馆ID的Claim类型

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="settlementRecordService">结算记录服务</param>
        public SettlementRecordController(ISettlementRecordService settlementRecordService)
        {
            _settlementRecordService = settlementRecordService;
        }

        /// <summary>
        /// 从认证令牌中获取场馆ID
        /// </summary>
        /// <returns>场馆ID</returns>
        private Guid GetVenueIdFromToken()
        {
            // 从令牌的Claims中获取场馆ID
            var venueIdClaim = User.Claims.FirstOrDefault(c => c.Type == VenueIdClaimType);
            
            if (venueIdClaim != null && Guid.TryParse(venueIdClaim.Value, out Guid venueId))
            {
                return venueId;
            }
            
            throw new UnauthorizedAccessException("未能从认证令牌中获取有效的场馆ID");
        }

        /// <summary>
        /// 获取所有结算记录
        /// </summary>
        /// <param name="queryParams">查询参数</param>
        /// <returns>结算记录列表</returns>
        [HttpPost("query")]
        public async Task<PageModel<SettlementRecordDto>> QuerySettlementRecords([FromBody] SettlementRecordQueryParams queryParams)
        {
            // 从认证令牌中获取场馆ID
            queryParams.VenueID = GetVenueIdFromToken();
            
            return await _settlementRecordService.QuerySettlementRecordsAsync(queryParams);
        }

        /// <summary>
        /// 获取场地预约结算记录
        /// </summary>
        /// <param name="queryParams">查询参数</param>
        /// <returns>场地预约结算记录列表</returns>
        [HttpPost("field")]
        public async Task<PageModel<FieldSettlementRecordDto>> QueryFieldSettlementRecords([FromBody] SettlementRecordQueryParams queryParams)
        {
            // 从认证令牌中获取场馆ID
            queryParams.VenueID = GetVenueIdFromToken();
            
            return await _settlementRecordService.QueryFieldSettlementRecordsAsync(queryParams);
        }

        /// <summary>
        /// 获取活动结算记录
        /// </summary>
        /// <param name="queryParams">查询参数</param>
        /// <returns>活动结算记录列表</returns>
        [HttpPost("activity")]
        public async Task<PageModel<ActivitySettlementRecordDto>> QueryActivitySettlementRecords([FromBody] SettlementRecordQueryParams queryParams)
        {
            // 从认证令牌中获取场馆ID
            queryParams.VenueID = GetVenueIdFromToken();
            
            return await _settlementRecordService.QueryActivitySettlementRecordsAsync(queryParams);
        }

        /// <summary>
        /// 获取结算记录详情
        /// </summary>
        /// <param name="id">结算记录ID</param>
        /// <returns>结算记录详情</returns>
        [HttpGet("{id}")]
        public async Task<ApiResult<SettlementRecordDto>> GetSettlementRecordById(int id)
        {
            return await _settlementRecordService.GetSettlementRecordByIdAsync(id);
        }

        /// <summary>
        /// 获取场馆结算统计
        /// </summary>
        /// <param name="startTime">开始时间（可选）</param>
        /// <param name="endTime">结束时间（可选）</param>
        /// <returns>场馆结算统计</returns>
        [HttpGet("stats")]
        public async Task<ApiResult> GetVenueSettlementStats(DateTime? startTime = null, DateTime? endTime = null)
        {
            // 从认证令牌中获取场馆ID
            Guid venueId = GetVenueIdFromToken();
            
            return await _settlementRecordService.GetVenueSettlementStatsAsync(venueId, startTime, endTime);
        }
    }
}